<?php

namespace Common\Model;

/**
 * 会员模型类
 * @author Administrator
 *
 */
class MemberModel extends CommModel {

    // 自动验证
    protected $_validate = array(
        array( 'username', '1,20', '请填写用户名!', self::EXISTS_VALIDATE, 'length' ),
    );

    // 自动完成
    protected $_auto = array(
        array( 'username', 'filter_label', self::MODEL_BOTH, 'function' ),
    );

    // 根据推荐码检测会员并返回会员信息
    public function checkUser( $data ) {
    	$return = returnArray( array( 'status' => 0, 'info' => '没有数据!' ) );
    	if ( !empty( $data['val'] ) ) {
    		$tempArray = $this->where( array( 'recommend' => $data['val'] ) )->field( "username,nickname,tel,sex,email,qq" )->find();
    		if ( count( $return['info'] ) > 0 ) {
    			$return['status'] = 1;
    			$return['info'] = $tempArray;
    		}
    	}
    	return $return;
    }

    // 格式化数据列表
    public function format( $data ) {
    	for ( $i = 0, $size = count( $data ); $i < $size; $i ++ ) {
    		$data[$i]['create_time'] = date( 'Y-m-d H:i:s', $data[$i]['create_time'] );
    		$data[$i]['update_time'] = date( 'Y-m-d H:i:s', $data[$i]['create_time'] );
    		$data[$i]['last_login_time'] = date( 'Y-m-d H:i:s', $data[$i]['last_login_time'] );
    		$data[$i]['last_login_ip'] = long2ip( $data[$i]['last_login_ip'] );
    	}
    	return $data;
    }

	protected $countList = 0;
	public function getSubList( $recommend, $type ) {
		if ( $type == "push" ) {
			return $this->where( array( 'recommended' => $recommend ) )->count();
		} else {
			$newArray = $this->where( array( 'recommended' => $recommend ) )->select();
			for ( $i = 0, $size = count( $newArray ); $i < $size; $i ++ ) {
				if ( $newArray[$i]['level'] > 1 ) {
					$this->countList ++;
				}
				$this->getSubList( $newArray[$i]['recommend'], $type );
			}
			return $this->countList;
		}
	}

}